<?php
/**
 * Created by PhpStorm.
 * Author:王晓云
 * Date: 2017/12/22 0022
 * Time: 20:15
 */
        namespace app\admin\controller;
        use app\admin\model\Article;
        use think\Controller;
        class News extends Comm
        {
          //新闻列表
          public function newsList(){
          $news=new Article;
          $list=$news->order("id","desc")->paginate(10);
          $this->assign("list",$list);
          return $this->fetch();
          }
          //新闻添加
          public function addNews(){
              if(request()->isPost()){
                  $data=input("post.");
                  //dump($data);
                  //die();
                  $news=new Article;
                  if($res=$news->allowField(true)->save($data)){
                      $this->success("文章添加成功！","News/newsList");
                  }else{
                      $this->error("文章添加失败！");
                  }
                  return;
              }
              $cat=$this->get_cat();
              $this->assign("cat",$cat);
              return $this->fetch();
          }
          //新闻图片上传
          public function addNewsPic(){
              if(request()->isPost()){
                  $file=request()->file('myfile');
                  if($file){
                      $info = $file->move(ROOT_PATH . 'public' . DS .'static'.DS.'images'.DS. 'uploads');
                      if($info){
                          // 成功上传后 获取上传信息
                          // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                          $res=$info->getSaveName();
                          return $res;
                      }else{
                          // 上传失败获取错误信息
                          echo $file->getError();
                      }
                  }
              }
          }
          //新闻修改
           public function editNews(){
              if(request()->isPost()){
                  $id=input("post.id");
                  $data=input("post.");
                  $news=new Article;
                  if($res=$news->allowField(true)->save($data,['id'=>$id])){
                      $this->success("新闻修改成功！","News/newsList");
                  }else{
                      $this->error("新闻修改失败！");
                  }
              }
               $cat=$this->get_cat();
               $this->assign("cat",$cat);
              $id=input("get.id");
              $news=new Article;
              $res=$news->where("id",$id)->find();
              $this->assign("res",$res);
              //dump($id);
            return  $this->fetch();
           }
           //新闻删除
            public function delNews(){
               $id=input('get.id');
               $news=new Article;
               if($news->where("id",$id)->delete()){
                   $this->success("新闻删除成功！","News/newsList");
               }else{
                   $this->error("新闻删除失败！");
               }
            }
            //新闻类别添加
            public function articleCatAdd(){
                if(request()->isPost()){
                  $name=input("post.name");
                  $pid=input("post.pid");
                  if($res=\think\Db::name('article_cat')->data(["name"=>$name,"pid"=>$pid])->insert()){
                     $this->success("新闻类别添加成功！","News/articleCat");
                  }else{
                      $this->error("新闻类别添加失败！");
                  }
                }
                $cat=$this->get_cat();
                $this->assign("list",$cat);
                return $this->fetch();
            }
            //新闻类别列表
            public function articleCat(){
                $res=$this->get_cat();
                $this->assign("list",$res);
                return $this->fetch();
            }

            //获取无限级分类
            public function get_cat($pid=0,&$res=array(),$spa=0){
                $spa=$spa+2;
                $data=\think\Db::name("article_cat")
                    ->where("pid",$pid)
                    ->select();
                 //dump($data);
                if($data) {
                    foreach ($data as $v) {
                        $v['name'] = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $spa) . '|-' . $v['name'];
                        $res[] = $v;
                        $this->get_cat($v['id'], $res, $spa);
                    }
                }
              return $res;
            }


        }


        ?>